import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';

const routes: RouteRecordRaw[] = [
    {
        path: '/workflow',
        name: 'workflow-list',
        component: () => import('@/views/workflow/WorkflowList.vue')
    },
    {
        path: '/workflow/create',
        name: 'workflow-create',
        component: () => import('@/views/workflow/WorkflowEdit.vue'),
        props: { isCreate: true }
    },
    {
        path: '/workflow/:id/edit',
        name: 'workflow-edit',
        component: () => import('@/views/workflow/WorkflowEdit.vue'),
        props: true,
        beforeEnter: (to, from, next) => {
            const id = to.params.id;
            if (!id || id === 'undefined') {
                next('/workflow');
            } else {
                next();
            }
        }
    },
    {
        path: '/',
        redirect: '/templates'
    },
    {
        path: '/templates',
        name: 'templates',
        component: () => import('@/views/template/TemplateList.vue')
    },
    {
        path: '/admin',
        component: () => import('@/layouts/AdminLayout.vue'),
        children: [
            {
                path: 'workflows',
                name: 'WorkflowManagement',
                component: () => import('@/views/admin/WorkflowManagement.vue'),
                meta: { title: '工作流管理' }
            }
        ]
    },
    {
        path: '/workflow/:id',
        name: 'WorkflowPreview',
        component: () => import('@/views/workflow/WorkflowPreview.vue'),
        props: true,
        beforeEnter: (to, from, next) => {
            const id = to.params.id;
            if (!id || id === 'undefined') {
                next('/workflow');
            } else {
                next();
            }
        }
    }
];

export default createRouter({
    history: createWebHistory(),
    routes
});